using Gtk;
using System;

using Serpis;

namespace PArticulo
{
	[ValidatorView]
	public partial class ArticuloView : Gtk.Window
	{
		//**	Declaramos el evento Changed
		public event PropertyChangedHandler Changed;
		//**	Declaramos un delegado para detectar los cambios en las propiedades
		public delegate void PropertyChangedHandler(object obj, string propertyName);
		
		public ArticuloView () : base(Gtk.WindowType.Toplevel)
		{
			this.Build ();
			
			precio.Adjustment.Lower = int.MinValue;
			precio.Adjustment.Upper = int.MaxValue;
			precio.Adjustment.StepIncrement=0.5;
			precio.Digits = 2;
			
			
			categoria.Init(AppDbConnection.Instance.DbConnection, "select id, nombre from categoria");
			categoria.Id = "";
			
			nombre.Changed += delegate { raiseChanged("Nombre");};
			precio.Changed += delegate { raiseChanged("Precio");};
			categoria.Changed += delegate { raiseChanged("Categoria");};	
		}
		//**	Nos agregamos al evento Changed
		protected virtual void raiseChanged(string propertyName)
		{
			if(Changed != null)
				Changed(this, propertyName);
				Console.WriteLine("Cambio en "+propertyName);
		}

		
		[ModelProperty]
		public string Nombre {
			get {return nombre.Text;}
			set {nombre.Text = value ?? "";}
		}
		
		[ModelProperty]
		public decimal Precio {
			get {return Convert.ToDecimal(precio.Value);}
			set {precio.Value = Convert.ToDouble(value);}
		}
		
		[ModelProperty]
		public long? Categoria {
			get {return categoria.Id == "" ? (long?)null : Convert.ToInt64(categoria.Id);}
			set {categoria.Id = value.ToString();}
		}
		
		public Gtk.Action SaveAction {
			get {return saveAction;}
		}
	}
}
